<template>
  <div class="auto">
    <modal :show.sync="show" title="表具信息"  v-ref:modal large backdrop="false">
      <article slot="modal-body" class="modal-body">
        <div style="height: 600px">
          <criteria-paged :model="model" v-ref:paged>
            <criteria partial='criteria' @condition-changed='$parent.$parent.selfsearch' v-ref:cri>
              <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
                <div class="row">
                  <div class="col-sm-3">
                    <label class="font_normal_body">使用公司</label>
                    <res-select
                      @res-select="$parent.$parent.$parent.getorg1"
                      :is-mul=false
                      restype='organization'>
                    </res-select>
                  </div>
                  <div class="span" style="float:right;">
                    <button class="button_search" @click="$parent.$parent.$parent.search()" v-el:cx>查询</button>
                    <button class="button_search" @click="$parent.$parent.$parent.showAllModal()" v-el:cx>新增</button>
                  </div>
                </div>
              </div>
            </criteria>
            <data-grid  :model="model" partial='list' v-ref:grid  class="list_area table_sy" >
              <template partial='head'>
                <tr>
                  <th><nobr>序号</nobr></th>
                  <th><nobr>气表品牌</nobr></th>
                  <th><nobr>使用公司</nobr></th>
                  <th><nobr>操作</nobr></th>
                </tr>
              </template>
              <template partial='body'>
                <tr>
                  <td style="text-align:center"><nobr>{{$index+1}}</nobr></td>
                  <td style="text-align:center"><nobr>{{row.f_meter_brand}}</nobr></td>
                  <td style="text-align:center"><nobr>{{row.f_orgname}}</nobr></td>
                  <td style="text-align:center">
                    <button class="btn btn-link "  @click.stop="$parent.$parent.$parent.$parent.delMeter(row)">删除</button>
                  </td>
                </tr>
              </template>
              <template partial='foot'></template>
            </data-grid>
          </criteria-paged>
        </div>
      </article>
      <footer slot="modal-footer" class="modal-footer">

      </footer>
    </modal>
  </div>
  <modal :show.sync="allshow" title="新增使用公司"   large backdrop="false" v-if="allshow">
    <article slot="modal-body" class="modal-body">
      <div class="form-horizontal select-overspread container-fluid auto ">
        <div class="row">
        <div class="col-sm-6">
        <label class="font_normal_body">使用公司</label>
        <res-select
          @res-select="getorg"
          :is-mul=false
          restype='organization'>
        </res-select>
      </div>
          <div class="col-sm-6">
        <div class="span" style="float:right;">
          <button type="button" class="button_search"  @click.stop="add" :disabled='orgids ==""'>保存</button>
        </div>
          </div>
      </div>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">

    </footer>
  </modal>
</template>
<script>

  import {PagedList} from 'vue-client'

  let addToOrg = async function (self) {
    let param = {
      f_gasbrand_id: self.f_gasbrand_id,
      f_using_orgid: self.orgids[0]
    }
    self.$resetpost('rs/logic/addBrandOrg', param, {resolveMsg: '添加成功', rejectMsg: '添加失败'})
    self.allshow =false
    self.search()
  }

  export default {
    title: '表具信息',
    data () {
      return {
        model: new PagedList('rs/sql/getAllBrands', 20),
        orgids:'',
        orgid:'',
        allshow:false,
        orgname:''
      }
    },
    props: ['show', 'f_gasbrand_id'],
    watch: {
      'show' (val) {
        if (val) {
          this.search()
        }
      },
      'allshow'(val){
        console.log("=======")
        if(!val) {
          this.orgids = ''
          console.log(this.orgids =="")
        }
        console.log(this.orgids =="")
      }
    },
    ready(){
      console.log(this.orgids =="","=++++")
    },
    methods: {
      getorg (orgid, orgname) {
        console.log(orgid)
        this.orgids = orgid
        this.orgname = orgname
      },
      getorg1 (orgid, orgname) {
        console.log(orgid)
        this.orgid = orgid
      },
      showAllModal(){
        this.allshow=true
      },
      search() {
        this.$refs.paged.$refs.cri.search()
      },
      selfsearch(args){
        args.condition = args.condition+ ' and g.id = '+this.f_gasbrand_id
        console.log(this.orgid,"=====================")
        if(this.orgid != ''){
          args.condition = args.condition+ ' and oz.id = '+this.orgid[0]
        }
        this.model.search(args.condition, args.model)
      },
      add() {
        let _this = this
        this.$showMessage(`确定要将气表品牌添加到分公司【${_this.orgname}】吗？`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            addToOrg(this)
          }
        })
      },
      delMeter (row) {
        this.$showMessage(`确定要将气表品牌${row.f_meter_brand}移除分公司【${row.f_orgname}】吗？`, ['confirm', 'cancel']).then((res) => {
          if (res === 'confirm') {
            this.$LogicService.deleteUsingOrg(row)
              .then(
                (res) => {
                  this.search()
                }
              ).catch(
              (error) => {
                this.$dispatch('error', '气表品牌删除', this.data, error)
              }
            )
          }
        })
      }
    },
  }

</script>
